package Demo;
import org.testng.annotations.Test;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;
import org.testng.asserts.*;



public class Kayak extends DemoClass {
	@Test
	public void testKayak() throws Exception
	{
		//beforelaunchBrowser();
		//System.setProperty("webdriver.chrome.driver", "C:\\Manjusha\\Selenium\\ChromeDriver\\chromedriver.exe");
	   //myWD = new ChromeDriver();
		myWD.get("http://www.kayak.com");
	  // myWD.navigate().to("http://www.kayak.com");
	   myWD.findElement(By.cssSelector("html.no-touch body div.fdLiner div#doc3Outer.yui-t2 div#bd div#bd-main.bdMain div.fdSearchForm div.fdMainLeft form#searchform div.fieldBlock div.fieldInput input#origin.autocomplete")).clear();
	   myWD.findElement(By.cssSelector("html.no-touch body div.fdLiner div#doc3Outer.yui-t2 div#bd div#bd-main.bdMain div.fdSearchForm div.fdMainLeft form#searchform div.fieldBlock div.fieldInput input#origin.autocomplete")).sendKeys("LAX");
	   myWD.findElement(By.cssSelector("html.no-touch body div.fdLiner div#doc3Outer.yui-t2 div#bd div#bd-main.bdMain div.fdSearchForm div.fdMainLeft form#searchform div.fieldBlock div.fieldInput input#destination.autocomplete")).sendKeys("DTW");
	   myWD.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS) ;
	   myWD.findElement(By.cssSelector("html.no-touch body div.fdLiner div#doc3Outer.yui-t2 div#bd div#bd-main.bdMain div.fdSearchForm div.fdMainLeft form#searchform div#departreturn div.fieldBlock div.fieldInput input#depart_date.datePicker")).clear();
	   myWD.findElement(By.xpath("//*[@id='depart_date']")).click();
	   
	   new WebDriverWait(myWD, 25).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("html.no-touch body div.fdLiner div#doc3Outer.yui-t2 div#bd div#bd-main.bdMain div.fdSearchForm div.fdMainLeft form#searchform div#departreturn div.fieldBlock div.fieldInput input#depart_date.datePicker")));
	
	   System.out.println("Month is:"+myWD.findElement(By.xpath("/html/body/div[8]/div/div/div/span")).getText());
	   System.out.println("Year is:"+myWD.findElement(By.xpath("/html/body/div[8]/div/div/div/span[2]")).getText());
	   myWD.findElement(By.xpath("//*[@id='depart_date']")).sendKeys("03/27/2013");
	  /* String xP1 = "/html/body/div[8]/div/table/tbody/tr[" ;
	   String xP2=	"]/td[";
		String xP3= "]";
		String xP4;*/
		
		/*String xP1 = "//html/body/div[8]/div/table/tbody/tr[";
		String xP2 = "]/td[";
		String xP3 = "]";
		String xP4;
		
		myWD.manage().timeouts().implicitlyWait(6, TimeUnit.SECONDS);
			
	
		for(int i=2;i<6;i++)
		{
			for(int j=2;j<7;j++)
			{
				xP4 = xP1+i+xP2+j+xP3;
				//xP9=xP1+i+xP2+j+xP8;
				try {
					System.out.println("Date in Row"+i+" Col"+j+" is " + myWD.findElement(By.xpath(xP4)).getText());
						if(i==4 && j==3){
							myWD.findElement(By.xpath(xP4)).click();
						}
						
					}
					catch (NoSuchElementException e) {
						System.out.println("Date in row"+i+" and col"+j+"is blank");
					}
					
			}
		}*/
	WebElement myWE = myWD.findElement(By.xpath("//*[@id='depart_time']"));
	Select myS = new Select(myWE);
	List<WebElement> myList = myWD.findElements(By.xpath("//*[@id='depart_time']"));
	for(WebElement myE: myList)
	{
		System.out.println("My list:" +myE.getText());
		if(myE.getText().equals("Anytime")){
			myE.click();
		}
	}
	 WebElement  myRWE = myWD.findElement(By.xpath("//*[@id='return_time']"));
	 Select myRS = new Select(myRWE);
	 List<WebElement> myRL =  myWD.findElements(By.xpath("//*[@id='return_time']"));
	 for(WebElement wE: myRL){
		 System.out.println("The List Return:"+wE.getText());
		 if(wE.getText().equals("Early (4a-8a)")){
			 try
			 {
				 wE.click();
			 }
			 catch (NoSuchElementException e) {
					System.out.println("No such element found");
				}
		 }
	 }
	myWE.findElement(By.xpath("//*[@id='return_date']")).clear();
	myWE.findElement(By.xpath("//*[@id='return_date']")).click();
	new WebDriverWait(myWD, 25).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("html.no-touch body div.fdLiner div#doc3Outer.yui-t2 div#bd div#bd-main.bdMain div.fdSearchForm div.fdMainLeft form#searchform div#departreturn div.fieldBlock div.fieldInput input#return_date.datePicker")));
	System.out.println("Month is:"+myWD.findElement(By.xpath("/html/body/div[8]/div[2]/div/div/span")).getText());
	  myWD.findElement(By.xpath("//*[@id='return_date']")).sendKeys("04/10/2013"); 
	/* String xP5 = "//html/body/div[8]/div[2]/table/tbody/tr[" ;
	 String xP6= "]/td[";
	 String xP7= "]";
	 String xP8;
	 myWD.manage().timeouts().implicitlyWait(6, TimeUnit.SECONDS);
	 
	 for (int i=2;i<6;i++)
	 {
		 for(int j=2;j<8;j++)
		 {
			 try{
			 xP8 = xP5+i+xP6+j+xP7;
			 System.out.println("Date in Row"+i+" Col"+j+" is " +myWE.findElement(By.xpath(xP8)).getText());
			 if(i==3 && j==6){
					myWD.findElement(By.xpath(xP8)).click();
				}
			 }
			 catch (NoSuchElementException e) {
					System.out.println("Date in row"+i+" and col"+j+"is blank");
				}
				
		 }
		 
	 }*/

	 
	 //Select how many adults
	 WebElement  myNWE = myWD.findElement(By.xpath("//*[@id='travelers']"));
	// Select myNS = new Select(myNWE);
	 List<WebElement> myNL =  myWD.findElements(By.xpath("//*[@id='travelers']"));
	 for(WebElement wE: myNL){
		 System.out.println("The List Return:"+wE.getText());
		 if(wE.getText().equals("3 adults")){
			 try
			 {
				 wE.click();
				 System.out.println(wE.getAttribute("selected"));
			 }
			 catch (NoSuchElementException e) {
					System.out.println("No such element found");
				}
		 }
	 }
	 
	
	 if(myWD.findElement(By.xpath("//*[@id='cbKAYAK_FDHCMP2']")).isSelected()){
		 myWD.findElement(By.xpath("//*[@id='cbKAYAK_FDHCMP2']")).click();
	 }
	/* myWD.findElement(By.linkText("//*[@id='showPTCOptionsLink']")).click();
	 Thread.sleep(6000);*/
	 
	 //Select Class
	 
	 WebElement  myTCWE = myWD.findElement(By.xpath("//*[@id='cabin']"));
	 Select myTCS = new Select(myNWE);
	 
	 List<WebElement> myTCL =  myWD.findElements(By.xpath("//*[@id='cabin']"));
	 
	 for(WebElement wTCE: myTCL)
	 {
		 String text = wTCE.getText();
		 System.out.println("The List Return:"+text);
		 if(wTCE.getText().equals("Economy")){
			 try
			 {
				 wTCE.click();
			 }
			 catch (NoSuchElementException e) {
					System.out.println("No such element found");
				}
		 }
	 }
	 myWD.findElement(By.xpath("//*[@id='fdimgbutton']")).click();

	 
	Set<String> winId = myWD.getWindowHandles();
	Iterator<String> winIds = winId.iterator();
	List<String> winIdL = new ArrayList();
	for(int i=0;i<winId.size();i++){
		//System.out.println("Window id:"+winIds.next());
		winIdL.add(winIds.next());
	}
	Thread.sleep(7000);
	 myWD.switchTo().window(winIdL.get(0));
	 Thread.sleep(7000);
	/*Alert alert = myWD.switchTo().alert();
	 alert.dismiss();*/
	 String dollar= myWD.findElement(By.xpath("/html/body/div/div[2]/div/div/div/div[2]/div[2]/div/div[8]/div/div/div/div/div/a")).getText();
	 System.out.println("Dollar is:"+dollar);
	 
	//Take pic of failed steps
	 File pic = ((TakesScreenshot) myWD).getScreenshotAs(OutputType.FILE);
	 String myPath = "C:/Manjusha/Selenium/FreeSeleniumTraining/test.jpg";
	 FileUtils.copyFile(pic, new File(myPath));
	 
	// aftercloseBrowser();
	 myWD.close();
	 myWD.quit();
	/* myWD.switchTo().window(winIdL.get(1));
	 Thread.sleep(6000);
	 myWD.close();*/
	 
    	}

}
